package com.mt.channel.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.entity.ChannelAccountEntity;
import com.mt.channel.entity.ChannelAccountLogEntity;
import com.mt.channel.entity.ChannelApplyDropCourseEntity;
import com.mt.channel.entity.ChannelOrderEntity;
import com.mt.channel.mapper.ChannelAccountEntityMapper;
import com.mt.channel.mapper.ChannelAccountLogEntityMapper;
import com.mt.channel.mapper.ChannelApplyDropCourseEntityMapper;
import com.mt.channel.mapper.ChannelOrderEntityMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;

@Component
public class ChannelAccountServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelAccountServiceModel.class);

    @Autowired
    private ChannelAccountEntityMapper channelAccountEntityMapper;

    @Autowired
    private ChannelAccountLogEntityMapper channelAccountLogEntityMapper;

    @Autowired
    private ChannelOrderEntityMapper channelOrderEntityMapper;

    @Autowired
    private ChannelApplyDropCourseEntityMapper channelApplyDropCourseEntityMapper;


    public ServiceResult<List<ChannelAccountEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelAccountEntityMapper, "Property 'channelAccountEntityMapper' is required.");
        ServiceResult<List<ChannelAccountEntity>> result = new ServiceResult<List<ChannelAccountEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            result.setResult(this.channelAccountEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAccountServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ChannelAccountEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelAccountEntityMapper, "Property 'channelAccountEntityMapper' is required.");
        ServiceResult<ChannelAccountEntity> result = new ServiceResult<ChannelAccountEntity>();
        try {
            result.setResult(this.channelAccountEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAccountServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ChannelAccountEntity channelAccountEntity) {
        Assert.notNull(this.channelAccountEntityMapper, "Property 'channelAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelAccountEntityMapper.insert(channelAccountEntity);
            if (save > 0) {
                id = channelAccountEntity.getChannelAccountId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAccountServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public ServiceResult<Integer> updateRelate(ChannelAccountEntity channelAccountEntity) {
        Assert.notNull(this.channelAccountEntityMapper, "Property 'channelAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelAccountEntityMapper.updateByPrimaryKeySelective(channelAccountEntity);
            if (save > 0) {
                id = channelAccountEntity.getChannelAccountId();
                List<ChannelAccountLogEntity> channelAccountLogList = channelAccountEntity.getChannelAccountLogList();
                if (channelAccountLogList != null && channelAccountLogList.size() > 0) {
                    channelAccountLogEntityMapper.insertList(channelAccountLogList);
                }
                List<ChannelOrderEntity> channelOrderEntityList = channelAccountEntity.getChannelOrderEntityList();
                if (channelOrderEntityList != null && channelOrderEntityList.size() > 0) {
                    channelOrderEntityMapper.insertList(channelOrderEntityList);
                }

            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAccountServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ChannelAccountEntity channelAccountEntity) {
        Assert.notNull(this.channelAccountEntityMapper, "Property 'channelAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelAccountEntityMapper.updateByPrimaryKeySelective(channelAccountEntity);
            if (save > 0) {
                id = channelAccountEntity.getChannelAccountId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAccountServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer channelAccountId) {
        Assert.notNull(this.channelAccountEntityMapper, "Property 'channelAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelAccountEntityMapper.deleteByPrimaryKey(channelAccountId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAccountServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public ServiceResult<Integer> channelApplyDropCourseUpdateRelate(ChannelAccountEntity channelAccountEntity,
            ChannelOrderEntity channelOrderEntity, ChannelApplyDropCourseEntity channelApplyDropCourseEntity) {
        Assert.notNull(this.channelAccountEntityMapper, "Property 'channelAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.channelAccountEntityMapper.updateByPrimaryKeySelective(channelAccountEntity);
            if (save > 0) {
                List<ChannelAccountLogEntity> channelAccountLogList = channelAccountEntity.getChannelAccountLogList();
                if (channelAccountLogList != null && channelAccountLogList.size() > 0) {
                    int i = channelAccountLogEntityMapper.insertList(channelAccountLogList);
                    if (i > 0) {
                        Integer updateResult = channelOrderEntityMapper.updateByPrimaryKeySelective(channelOrderEntity);
                        if (updateResult > 0) {
                            channelApplyDropCourseEntityMapper
                                    .updateByPrimaryKeySelective(channelApplyDropCourseEntity);
                        }
                    }
                }
            }
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAccountServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

}
